package test.jinggame;

public class Solution {
    public static void main(String[] args) {
        String[] board = {
                "OOXXOXXX",
                "XXXOXOXO",
                "OXOXXXOO",
                "XOXOXXXX",
                "OXOOXOOO",
                "XOOOOOOO",//
                "OXXXOOOX",
                "XOXOOXXX"};
        String tictactoe = new Solution().tictactoe(board);
        System.out.println(tictactoe);
    }

    public String tictactoe(String[] board) {
        /*
         *
         * [
         * "O X",
         * " XO",
         * "X O"]

         * */
        if (board.length == 1 && !board[0].equals(" ")) {
            return board[0];
        }
        int length = board.length;
        for (int k = 0; k < 2; k++) {
            for (int i = 0; i < length + 2 * k; i++) {
                int j;
                char first;
                char second;
                if (k == 1) {
                    if (i < length) {
                        second = board[i].charAt(0);//i 0, 0 i, 0 0, 0 length-1
                    } else if (i == length) {
                        second = board[0].charAt(0);
                    } else {
                        second = board[0].charAt(length - 1);
                    }
                } else {
                    second = board[0].charAt(i);
                }
                for (j = 0; j < length - 1; j++) {
                    first = second;
                    if (k == 1) {
                        if (i < length) {
                            second = board[i].charAt(j + 1);//i j+1, j+1 i, j+1 j+1, j+1 length-1-(j + 1)
                        } else if (i == length) {
                            second = board[j + 1].charAt(j + 1);
                        } else {
                            second = board[j + 1].charAt(length - 1 - (j + 1));
                        }
                    } else {
                        second = board[j + 1].charAt(i);
                    }

                    if (first == ' ' || first != second) {
                        break;
                    }
                }
                if (j == length - 1) {
                    //有胜利者
                    return String.valueOf(second);
                }
            }
        }
        boolean isDraw = true;
        for (String s : board) {
            if (s.contains(" ")) {
                isDraw = false;
                break;
            }
        }
        if (isDraw) {
            //全部都填满，平局
            return "Draw";
        }

        //还可继续
        return "Pending";
        /*
        * 输入： board =
        * ["O X",
        *  " XO",
        *  "X O"]
输出： "X"
示例 2：

输入： board = ["OOX","XXO","OXO"]
输出： "Draw"
解释： 没有玩家获胜且不存在空位
示例 3：

输入： board = ["OOX","XXO","OX "]
输出： "Pending"
解释： 没有玩家获胜且仍存在空位

        * */
    }
}
